package com.wc.算法提高课.D第四章_高级数据结构.Floyd算法.观光之旅;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/15 22:30
 * @description https://www.acwing.com/problem/content/346/
 */
public class Main {
    /**
     * 思路：floyd理解<p>
     * 需要深刻理解floyd的第一层 k 的含义, 遍历了中间点1 ~ k - 1, 即将遍历第k个点作为中间点的最短路<p>
     * 根据题意, 设定最短路的环中最大值为 k, 那么假设 i, j < k, 环距离为 d[i][k] + g[k][j] + g[i][j]
     * 如何保存路径, 假设pos[i][j]表示连接了i ~ j这条路的中的点 k, 且唯一, 就可以通过前序遍历的方式求出路径
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, INF = 0x3f3f3f3f;
    static int[][] d = new int[N][N], g = new int[N][N], pos = new int[N][N];
    static int[] path = new int[N];
    static int n, m, cnt = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            Arrays.fill(g[i], INF);
            g[i][i] = 0;
        }
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            g[a][b] = g[b][a] = Math.min(g[a][b], c);
        }
        for (int i = 1; i <= n; i++) {
            System.arraycopy(g[i], 1, d[i], 1, n);
        }
        int res = INF;
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i < k; i++) {
                for (int j = i + 1; j < k; j++) {
                    // 因为还未利用k连接i ~ k ~ j
                    if ((long) d[i][j] + g[i][k] + g[k][j] < res) {
                        res = d[i][j] + g[i][k] + g[k][j];
                        cnt = 0;
                        path[cnt++] = k;
                        path[cnt++] = i;
                        getPath(i, j);
                        path[cnt++] = j;
                    }
                }
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (d[i][j] > d[i][k] + d[k][j]) {
                        d[i][j] = d[i][k] + d[k][j];
                        pos[i][j] = k;
                    }
                }
            }
        }
        if (res == INF) out.println("No solution.");
        else {
            for (int i = 0; i < cnt; i++) out.print(path[i] + " ");
            out.println();
        }
        out.flush();
    }

    static void getPath(int i, int j) {
        // 这说明i ~ j未连接
        if (pos[i][j] == 0) return;
        int k = pos[i][j];
        getPath(i, k);
        path[cnt++] = k;
        getPath(k, j);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
